package com.mh.controller;

import com.mh.common.R;
import com.mh.service.DoctorInfoService;
import com.mh.service.StudentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;

/**
 * Date:2023/4/16
 * author:zmh
 * description: 通用接口
 **/

@RestController
@Slf4j
@RequestMapping("/commons")
@CrossOrigin
public class commonController {

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private DoctorInfoService doctorInfoService;

    /**
     * 登录检查-学生
     * @param userName 姓名
     * @param password 密码
     * @return 返回检查结果，返回code为1表示验证成功，返回code为0代表检查失败
     */
    @PostMapping("/loginCheckOfStudent")
    public R<String> checkLoginOfStudent(String userName, String password){
        // 调用学生信息表进行验证，否则调用医生信息表
            String studentInfo = studentInfoService.checkLoginByStudent(userName, password);
            return "登录成功!".equals(studentInfo)? R.success("登录成功!"): R.error(studentInfo);
    }

    /**
     * 登录检查-医生
     * @param userName 姓名
     * @param password 密码
     * @return 返回检查结果，返回code为1表示验证成功，返回code为0代表检查失败
     */
    @PostMapping("/loginCheckOfDoctor")
    public R<String> checkLoginOfDoctor(String userName, String password){
        // 调用学生信息表进行验证，否则调用医生信息表
        String doctorInfo = doctorInfoService.checkLoginByDoctor(userName, password);
        return "登录成功!".equals(doctorInfo)? R.success("登录成功!"): R.error(doctorInfo);
    }

    @Value("${picturePath}")
    private String picturePath;
    /**
     * 获取头像图片
     * @return 返回字节数据流
     * @throws IOException ·
     */
        @GetMapping("/image")
    public ResponseEntity<byte[]> getImage(String picName) throws IOException {
        // 拼接图片路径
        String randomPic = picturePath + picName;
        File file = new File(randomPic);
        byte[] bytes = Files.readAllBytes(file.toPath());
        return ResponseEntity.ok().body(bytes);
    }

}
